﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Runtime.Serialization;
using Imageo.bintje;
using System.Collections.Specialized;

namespace Imageo
{
    public partial class TrailTable : System.Web.UI.UserControl
    {
        #region Properties
        public Unit Width
        {
            set
            {
                pnlTrailsTableControl.Width = value;
            }
        }

        public Unit Height
        {
            set
            {
                pnlTrailsTableControl.Height = value;
            }
        }
        
        #endregion

        public bool ShowPager
        {
            get
            {
                Control c = ListView1.FindControl("rowPager");
                if (c != null) return c.Visible;
                return false;
            }
            set
            {
                Control c = ListView1.FindControl("rowPager");
                if (c != null) c.Visible=value;
            }
        }

        IList<bintje.trailWrapper> trails;
        public IList<bintje.trailWrapper> Trails
        {
            get { return trails; }
            set { trails = value; }
        }

        IList<trailBox> GetBoxes(IList<trailWrapper> trailList, int proUserId)
        {
            List<trailBox> boxes = new List<trailBox>();

            if (trails != null)
            {
                foreach (bintje.trailWrapper tw in trailList)
                {
                    trailBox box = new trailBox(tw, proUserId);
                    boxes.Add(box);
                }
            }
            
            return boxes;
        }

        public void LoadTrails(IList<bintje.trailWrapper> trailList)
        {
            trails = trailList;
            
            ListView1.DataSourceID = null;
            ListView1.DataSource = GetBoxes(trails, 0);
            ListView1.DataBind();

            ListView2.DataSourceID = null;
            ListView2.DataSource = GetBoxes(trails, 0);
            ListView2.DataBind();
        }

        
        protected void Page_Load(object sender, EventArgs e)
        {
            ListView1.Visible = false;
            ListView2.Visible = true;
        }

        public void SortByComments()
        {
            if (ListView1.DataSource != null)
            {
                var sortedTrails = (from tr in trails
                                    orderby tr.numberOfannotations
                                    select tr).ToArray();

                ListView1.DataSource = GetBoxes(sortedTrails, 0);
                ListView1.DataBind();
            }

            if (ListView2.DataSource != null)
            {
                var sortedTrails = (from tr in trails
                                    orderby tr.numberOfannotations
                                    select tr).ToArray();

                ListView2.DataSource = GetBoxes(sortedTrails, 0);
                ListView2.DataBind();
            }
        }

        public void SortByTitle()
        {
            if (ListView1.DataSource != null)
            {
                var sortedTrails = (from tr in trails
                                    orderby tr.title
                                    select tr).ToArray();

                ListView1.DataSource = GetBoxes(sortedTrails, 0);
                ListView1.DataBind();
            }

            if (ListView2.DataSource != null)
            {
                var sortedTrails = (from tr in trails
                                    orderby tr.title
                                    select tr).ToArray();

                ListView2.DataSource = GetBoxes(sortedTrails, 0);
                ListView2.DataBind();
            }
        }

        public void SortByDistance()
        {
            if (ListView1.DataSource != null)
            {
                var sortedTrails = (from tr in trails
                                    orderby tr.distance
                                    select tr).ToArray();

                ListView1.DataSource = GetBoxes(sortedTrails,0 );
                ListView1.DataBind();
            }

            if (ListView2.DataSource != null)
            {
                var sortedTrails = (from tr in trails
                                    orderby tr.distance
                                    select tr).ToArray();

                ListView2.DataSource = GetBoxes(sortedTrails, 0);
                ListView2.DataBind();
            }
        }

        public void SortBySimilarity()
        {
            if (ListView1.DataSource != null)
            {
                var sortedTrails = (from tr in trails
                                    orderby tr.similarityScore
                                    select tr).ToArray();

                ListView1.DataSource = GetBoxes(sortedTrails, 0);
                ListView1.DataBind();
            }

            if (ListView2.DataSource != null)
            {
                var sortedTrails = (from tr in trails
                                    orderby tr.similarityScore
                                    select tr).ToArray();

                ListView2.DataSource = GetBoxes(sortedTrails, 0);
                ListView2.DataBind();
            }
        }

        protected void ListView1_PagePropertiesChanged(object sender, EventArgs e)
        {
            ListView1.DataBind();
        }

        protected void ListView2_PagePropertiesChanged(object sender, EventArgs e)
        {
            ListView2.DataBind();
        }
    }
}